package com.proposition.web.paper;

import com.proposition.commons.constant.ErrorCode;
import com.proposition.commons.constant.PaperPermission;
import com.proposition.commons.model.StrategyOne;
import com.proposition.commons.model.StrategyTwo;
import com.proposition.paper.service.PaperStrategyService;
import com.proposition.support.utils.Rest;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.Map;

/**
 * ${DESCRIPTION}
 *
 * @author 杨凯
 * @create 2018-01-31 11:18
 **/
@Controller
@RequestMapping("/strategy")
public class PaperStrategyController {

	private static final Logger log = LoggerFactory.getLogger(PaperStrategyController.class);

	@Autowired
	private PaperStrategyService paperStrategyService;

	@GetMapping("/index")
	public String strategyPage(Model model) {
		model.addAttribute("strategy",paperStrategyService.getStrategyTwo().getStrategy());
		return "paper/paper-strategy";
	}

	@ResponseBody
	@GetMapping(value="/data")
	public Map<String,Object> getStrategyData(){
		Map<String,Object> map = new HashMap<String,Object>();
		//获取第一站组卷策略
		StrategyOne one = paperStrategyService.getStrategyOne();
		//获取第二站组卷策略
		StrategyTwo two = paperStrategyService.getStrategyTwo();
		map.put("one", one);
		map.put("two", two);
		return map;
	}

	@PostMapping("/add")
	@ResponseBody
	public Rest insertStrategy(int exam2sk,int exam2fk,int exam2ek,int exam2nk,int exam2cz,
	                           int exam3sk,int exam3fk,int exam3ek,int exam3nk,int exam3cz,
	                           String pattern2exam11, String pattern2exam12, String pattern2exam13, String pattern2exam14,
	                           String pattern2exam15, String pattern2exam21, String pattern2exam22,
	                           String pattern2exam23, String pattern2exam24, int examsk, int examek, int examfk, int examnk,int strategy) {
		Rest.Builder rest = Rest.newBuilder();
		try {
			Subject subject = SecurityUtils.getSubject();
			if(!subject.isPermitted(PaperPermission.ADD_PAPER)){
				//没有权限；
				return rest.status(ErrorCode.PERMISSION_DENIED).build();
			}
			StrategyOne one = new StrategyOne();
			//腧穴题的分类为 “1”
			one.setExam1("1");
			one.setExam2FK(exam2fk);
			one.setExam2EK(exam2ek);
			one.setExam2NK(exam2nk);
			one.setExam2SK(exam2sk);
			one.setExam2CZ(exam2cz);
			one.setExam3FK(exam3fk);
			one.setExam3EK(exam3ek);
			one.setExam3NK(exam3nk);
			one.setExam3SK(exam3sk);
			one.setExam3CZ(exam3cz);
			one.setCreated(new Timestamp(System.currentTimeMillis()));
			StrategyTwo two = new StrategyTwo();
			two.setExam1(StringUtils.join(new String[]{pattern2exam11, pattern2exam12, pattern2exam13, pattern2exam14, pattern2exam15}, ","));
			two.setExam2(StringUtils.join(new String[]{pattern2exam21, pattern2exam22, pattern2exam23, pattern2exam24}, ","));
			two.setExamEK(examek);
			two.setExamFK(examfk);
			two.setExamNK(examnk);
			two.setExamSK(examsk);
			two.setCreated(new Timestamp(System.currentTimeMillis()));
			two.setStrategy(strategy);
			paperStrategyService.insertStrategy(one, two);
		} catch (Exception e) {
			log.error("添加组卷策略出现异常[{}]", e.getMessage());
			return rest.status(ErrorCode.UNEXPECTED_ERROR).build();
		}
		return rest.status(ErrorCode.SUCCESS).build();
	}
}
